package com.googlecode.vanaraha.utils
import scala.collection.mutable
trait Observer[S]{
  def update(item: S): Judgement.Value
}
 object Judgement extends Enumeration{
    val ACCEPT, REJECT = Value
  }
trait SilentObserver[S] extends Observer[S]{
  override def update(item: S) = {
    inform(item)
    Judgement.ACCEPT
  }
  def inform(item: S):Unit 
  
}
  
trait ObserverSupporter[S] {
  import Utils.logger.info
  val observers = new mutable.ArrayBuffer[Observer[S]]
  
  def addObserver(x: Observer[S]) = {
    info("adding observer %s to object: %s", x, this.getClass.getSimpleName)
    observers +=x
  }
  def removeObserver(x: Observer[S]) ={
    info("removing observer %s from object %s", x, this.getClass.getSimpleName)
    observers -=x
  } 
  def removeAllObservers = observers.clear
  def notifyObservers(item: S) = {
    val result = observers.forall(_.update(item) == Judgement.ACCEPT)
    if(!result)
      info("In [%s] observers have rejected item with identity: #%s",getClass.getSimpleName, System.identityHashCode(item))
    result
 }
}


object Utils{
 val logger = new Logger(org.apache.commons.logging.LogFactory.getLog("VFILE"))
 import java.lang.System.currentTimeMillis
 import java.io.{InputStream, FileInputStream, BufferedInputStream}
   def timeIt(f: => Unit) = {
     val before = currentTimeMillis
     f //execute method
     currentTimeMillis - before
   }
   
   def timeIt(infoMsg: String)(f: => Unit): Unit = logger.info(infoMsg, timeIt(f)+" milliseconds")
   
   def %% (x: String, values: Any*) = x.format(values: _*)
   
   def countItems(iter: Iterator[_]) = {
     var count = 0
     while(iter.hasNext){ iter.next; count+=1}
     count
   }
   
   implicit def iterableToUnique[A](x: Iterable[A]) = new {
     def unique: Array[A] = (new mutable.HashSet ++ x).toArray
   }
   
   implicit def jIterToScalaIter[A](x: java.util.Iterator[A]) = new Iterator[A]{
     override def hasNext = x.hasNext
     override def next = x.next
   }
   
   implicit def predicateToComparator[A](x: (A,A) => Int) = new java.util.Comparator[A]{
     override def compare(item: A, item1: A) = x(item, item1)
   }
   
}

class SizeBoundedCollection[A](val maxCapacity: Int, predicate: (A,A) => Int) extends Iterable[A]{
  import scala.util.Sorting
  import java.util.{ArrayList, Collections}
  import Utils._
  private var items = new ArrayList[A]
  private var sorted = false
  private def slice(list: ArrayList[A], start:Int, end: Int) ={
    val list1 = new ArrayList[A]
    for(i <- start to end)
      list1.add(list.get(i))
    list1
  }

  private def removeUnwantedItems{
    if(!sorted){
       Collections.sort(items, predicate)
       sorted = true
    }
    if(items.size > maxCapacity)
    	items = slice(items, 0, maxCapacity)
  }
  
  override def elements = {
    removeUnwantedItems
    items.iterator
  }   
  def add(item: A)= {
    items.add(item)
    sorted = false
    if(items.size % 20 == 0){
      removeUnwantedItems
    }
  }
  def +=(item: A) = add(item)
  override def toString = elements.mkString("[\n\t", "\n\t","\n]")
}

trait PreferenceStore {
	def apply(str: String) : String
	def update(str: String, value: String)   
}
